package com.ruoyi.noDoor.service.impl;


import com.ruoyi.noDoor.domain.HouseNoDoorT;
import com.ruoyi.noDoor.mapper.HouseNoDoorTMapper;
import com.ruoyi.noDoor.service.IHouseNoDoorTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 户管理Service业务层处理
 * 
 * @author lijie
 * @date 2023-04-20
 */
@Service
public class HouseNoDoorTServiceImpl implements IHouseNoDoorTService
{
    @Autowired
    private HouseNoDoorTMapper houseNoDoorTMapper;

    private int floorNum = 0;

    private int doorNum = 0;


    @Override
    public List<HouseNoDoorT> selectHouseNoDoorTList(HouseNoDoorT houseNoDoorT)
    {
        return houseNoDoorTMapper.selectHouseNoDoorTList(houseNoDoorT);
    }

    @Override
    public HouseNoDoorT getDoorDetailTemp(HouseNoDoorT houseNoDoorT)
    {
        return houseNoDoorTMapper.getDoorDetailTemp(houseNoDoorT);
    }

    /**
     * 新增户管理
     * 
     * @param houseNoDoorT 户管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNoDoorInfo(HouseNoDoorT houseNoDoorT)
    {
        floorNum = 0;
        doorNum = 0;
        List<HouseNoDoorT> doorList = new ArrayList<HouseNoDoorT>();
        HouseNoDoorT door;
        int result = 0;
        houseNoDoorTMapper.deleteNoDoor(houseNoDoorT);
        doorList = houseNoDoorTMapper.selectHouseDoorTList(houseNoDoorT);
        this.getFloorAndDoor(doorList);
        for(int floorV = 1 ; floorV <= floorNum ;) {
            for(int doorrV = 1 ; doorrV <= doorNum ; doorrV++) {
                String doorId = "";
                doorId = String.valueOf(floorV) + String.format("%" + 2 + "d", doorrV).replace(" ", "0");

                door = new HouseNoDoorT();
                houseNoDoorT.setDoorName(doorId);
                door = houseNoDoorTMapper.getDoorDetailTemp(houseNoDoorT);
                if(door == null) {
                    result = houseNoDoorTMapper.insertNoDoor(houseNoDoorT);
                }
            }
            if(floorV == floorNum){
                result = 1;
            }
            floorV++;
        }

        return result;
    }
    /**
     * 新增户管理
     *
     * @param houseNoDoorT 户管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseDoorT(HouseNoDoorT houseNoDoorT)
    {
        return houseNoDoorTMapper.insertHouseDoorT(houseNoDoorT);
    }

    public void getFloorAndDoor(List<HouseNoDoorT> doorList){

        for(int j=0;j<doorList.size();j++){
            int floor = Integer.valueOf(doorList.get(j).getDoorName().substring(0, doorList.get(j).getDoorName().length()-2));
            if(floor>floorNum){
                floorNum = floor;
            }
            int door = Integer.valueOf(doorList.get(j).getDoorName().substring(doorList.get(j).getDoorName().length()-2,doorList.get(j).getDoorName().length()));
            if(door>doorNum){
                doorNum = door;
            }
        }
    }



}
